package com.example.tivi.ui;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.tivi.R;
import com.example.tivi.entity.Category;
import com.example.tivi.entity.GlobalInfor;
import com.example.tivi.imageloader.ImageLoader;

public class CategoryFragment extends Fragment {
	private final static String TAG = "CategoryFragment";
	private Button mbtnOtherApp;
	private ListView mlistCategory;

	private CategoryAdapter mCategoryAdapter;
	private OnCategoryClickListener mOnCategoryClickListener;
	
	public interface OnCategoryClickListener {
		public void onCategoryClick(int categoryID, String categoryName);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mOnCategoryClickListener = (OnCategoryClickListener) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_category, container,
				false);
		initView(view);
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
	}

	private void initView(View view) {
		mbtnOtherApp = (Button) view
				.findViewById(R.id.fragment_category_btn_other);
		mlistCategory = (ListView) view
				.findViewById(R.id.fragment_category_list_category);

		mlistCategory.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Category category = (Category) ((CategoryAdapter) arg0
						.getAdapter()).getItem(arg2);
				mOnCategoryClickListener.onCategoryClick(category.getId(),
						category.getName());
			}
		});

		mbtnOtherApp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
	}

	private void initData() {
		mCategoryAdapter = new CategoryAdapter(getActivity(), GlobalInfor.getInstance().getCategoryList());
		mlistCategory.setAdapter(mCategoryAdapter);
	}

	private class CategoryAdapter extends BaseAdapter {
		private final String TAG = "CategoryAdapter";
		private List<Category> categories;
		private Context mContext;
		private ImageLoader mImageLoader;

		public CategoryAdapter(Context context, List<Category> list) {
			mContext = context;
			categories = list;
			mImageLoader=new ImageLoader(mContext);
		}

		@Override
		public int getCount() {
			return categories.size();
		}

		@Override
		public Object getItem(int position) {
			return categories.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(mContext,
						R.layout.item_list_category, null);
				holder.imgThumnail = (ImageView) convertView
						.findViewById(R.id.item_list_category_img);
				holder.txtName = (TextView) convertView
						.findViewById(R.id.item_list_category_txt);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.txtName.setText(categories.get(position).getName());
			mImageLoader.DisplayImage(categories.get(position).getThumbnail(), holder.imgThumnail);
			return convertView;
		}

		private class ViewHolder {
			ImageView imgThumnail;
			TextView txtName;
		}

	}
}
